package nl.weeaboo.sh;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import nl.weeaboo.game.Log;
import nl.weeaboo.sh.level.MainMenu;
import nl.weeaboo.sh.level.l99.Level99;
import timon.common.io.FileUtil;

public class Launcher {

	//Functions
	public static void main(String args[]) {
		generateResourceZip();
		
		Config config = new Config();
		try {
			config.load("config.xml");
		} catch (IOException e) {
			Log.warning("Error loading config.xml :: " + e.toString());
		}
		            
		Game game = new Game(config);
		game.start();
		
		while (!game.isStarted()) {
			try {
				Thread.yield();
				Thread.sleep(100);
			} catch (InterruptedException e) {
			}
		}
		
		if (Game.debugSkipMenu) {
			game.setLevel(new Level99(game));
		} else {
			game.setLevel(new MainMenu(game));
		}
	}
	
	public static void generateResourceZip() {
		try {
			if (new File("res").exists()) {
				Map<String, File> files = new HashMap<String, File>();
				FileUtil.collectFiles(files, new File("res"), false);
				for (Iterator<Entry<String, File>> it = files.entrySet().iterator(); it.hasNext(); ) {
					Entry<String, File> entry = it.next();
					if (entry.getKey().contains(".svn/")) {
						it.remove();
					}
				}
				
				if (!files.isEmpty()) {
					FileUtil.zip(files, new File("res.zip"), null);
				}
			}
		} catch (IOException e) {
			Log.showError(e);
		}		
	}
	
	//Getters
	
	//Setters
	
}
